from flask import jsonify, request

from app.forms.book import SearchForm
from . import web
from app.libs.helper import is_isbn_or_key
from app.spider.yushu_book import YuShuBook


@web.route('/book/search')
def search():
    """
        q (/isbn) 普通关键字
        page(start, count)

    """
    form = SearchForm(request.args)
    if form.validate():
        q = form.q.data.strip()
        page = form.page.data
        # q = request.args['q']
        # page = request.args['page']
        isbn_or_key = is_isbn_or_key(q)
        if isbn_or_key == 'isbn':
            result = YuShuBook.search_by_isbn(q)
        else:
            result = YuShuBook.search_by_keyword(q,page)
        # return json.dumps(result), 200, {'content-type': 'application/json'}
        return jsonify(result)
    else:
        # return jsonify({'msg': '参数校验失败'})
        return jsonify(form.errors)
